package com.playball.lottery.nt.animal;

import com.playball.lottery.ApplicationData;
import com.playball.lottery.nt.GetJsonData;
import com.playball.lottery.nt.GetRequestParams;
import com.playball.lottery.util.ConvertJsonToList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Jay.Jia on 2/13/17.
 */

public class AnimalServiceImpl {

    private BaseServiceImpl baseService;

    public AnimalServiceImpl() {
        this.baseService = new BaseServiceImpl();
    }

    /**
     * 投注接口-number-list部分
     *
     * @param numArr
     * @return
     */
    public JSONArray getBetParamsStringList(List<Map<String, Object>> numArr) {
        JSONArray array1 = new JSONArray();
        for (int i = 0; i < numArr.size(); i++) {
            JSONObject o = new JSONObject(numArr.get(i));
            array1.put(o);
        }
        return array1;
    }

    /**
     * 投注接口-number部分
     *
     * @param playTypeId
     * @param number     投注内容+金额+赔率，中间用逗号连接
     * @param money
     * @return
     */
    public JSONArray getBetParamsString(String playTypeId, String number, String money) {

        List<Map<String, Object>> numArr = new ArrayList<Map<String, Object>>();

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("playid", playTypeId);
        map.put("number", number);
        map.put("money", money);

        numArr.add(map);

        JSONArray array1 = new JSONArray();
        for (int i = 0; i < numArr.size(); i++) {
            JSONObject o = new JSONObject(numArr.get(i));
            array1.put(o);
        }

        return array1;
    }

    /**
     * 完整的投注接口
     *
     * @param lotID
     * @param issueID   彩票期次Id
     * @param issueName 彩票期次
     * @param numberVal
     * @param allMoney
     * @return
     */
    public String getCompleteBetParams(
            int lotID, int issueID, String issueName, JSONArray numberVal, double allMoney) {
        String result;
        JSONObject params = new JSONObject();
        try {
            params.put("SchemeType", 1);// 方案类型 1自购，2合买，3保存 1
            params.put("BetType", 1);// 投注类型 1选号投注 2单式上传 3先发后传 1
            params.put("BonusScale", 0);
            params.put("AssureShare", 0);
            params.put("Description", "");
            params.put("OpenUserList", "");
            params.put("SecrecyLevel", 0);
            params.put("BuyShare", 1);
            params.put("Multiple", 1);
            params.put("LotteryID", lotID);
            params.put("IssueID", issueID);     // 彩票期次Id
            params.put("IssueName", issueName); // 彩票期次
            params.put("Number", numberVal);
            params.put("Money", 1 * allMoney);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        result = params.toString();
        return result;
    }

    /**
     * step 3. 拼接投注url－前缀
     *
     * @param paramArr
     * @param UserID
     * @param action
     * @return
     */
    public String getPrefixBetParams(String paramArr, String UserID, String action) {
        String result = "";
        JSONObject params = new JSONObject();
        try {
            String sign = GetRequestParams.getRequestSign(GetRequestParams.Uuid, "Android", android.os.Build.VERSION.RELEASE, ApplicationData.APPVERSION, paramArr, "8888");
            params.put("uuid", GetRequestParams.Uuid);
            params.put("platformCode", "Android");
            params.put("platformVersion", "6.0");
            params.put("appVersion", ApplicationData.APPVERSION);
            params.put("UserID", UserID);
            params.put("action", action);
            params.put("params", paramArr.replace("+", "%2B"));
            params.put("sign", sign);
            params.put("cmdId", ApplicationData.getInstance().getCommenderID());
            params.put("cmdName", "app_zz");
            // System.out.println("params is " + params.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Map<String, Object> mapParams = ConvertJsonToList.getMap(params.toString());
        // 拼接参数
        StringBuffer sb = new StringBuffer();
        for (Map.Entry<String, Object> entry : mapParams.entrySet()) {
            sb.append(entry.getKey()).append("=").append(entry.getValue().toString());
            sb.append("&");
        }
        result = sb.deleteCharAt(sb.length() - 1).toString();
        return result;
    }

    /**
     * 获取赔率
     *
     * @return
     */
    public String getResponseOfOdds() {
        String result = "";
        String urlOfBasic = baseService.getURLOfBasic(
                "NzU2", "6.0", "Android", "7E53583A0AB707DE486DADA0983DAEEA", "1.1.7",
                "10314", "app_zz", "000000000000000");
        String urlOfParams = baseService.getURLOfParams("");
        String url = baseService.prefixUrl + urlOfBasic + "&" + urlOfParams;
        // System.out.println("url is " + url);
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(url)
                .build();

        try {
            Response response = client.newCall(request).execute();
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //如果response回傳是null, 就代表timeout或 沒有網路

        return result;
    }

    /**
     * 获取赔率的URL
     *
     * @return
     */
    public String getURLOfOdds() {
        String urlOfBasic = baseService.getURLOfBasic(
                "NzU2", "6.0", "Android", "7E53583A0AB707DE486DADA0983DAEEA", "1.1.7",
                "10314", "app_zz", "000000000000000");
        String urlOfParams = baseService.getURLOfParams("");
        String url = baseService.prefixUrl + urlOfBasic + "&" + urlOfParams;
        return url;
    }

    /**
     * 获取最新期次信息
     *
     * @return
     */
    public String getResponseOfIssue() {
        String result = "";
        String url = baseService.prefixUrl + "UserID=NzU2&platformVersion=6.0&platformCode=Android&sign=7E53583A0AB707DE486DADA0983DAEEA&appVersion=1.1.7&action=10315&cmdName=app_zz&uuid=000000000000000&params={}";
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(url)
                .build();

        try {
            Response response = client.newCall(request).execute();
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //如果response回傳是null, 就代表timeout或 沒有網路

        return result;
    }

    /**
     * 获取中奖信息
     *
     * @return
     */
    public String getResponseOfWinning() {
        String result = "";

        // System.out.println(GetJsonData.urlPath);
        String url = baseService.prefixUrl + "UserID=NzU2&platformVersion=6.0&platformCode=Android&sign=7E53583A0AB707DE486DADA0983DAEEA&appVersion=1.1.7&action=301&cmdName=app_zz&uuid=000000000000000&params={\"LottID\":97}";
        System.out.println("url is " + url);
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(url)
                .build();

        try {
            Response response = client.newCall(request).execute();
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //如果response回傳是null, 就代表timeout或 沒有網路

        return result;
    }

    /**
     * 获取开奖的URL
     *
     * @return
     */
    public String getURLOfWinning() {
        String result;

        // System.out.println(GetJsonData.urlPath);
        String url = baseService.prefixUrl + "UserID=NzU2&platformVersion=6.0&platformCode=Android&sign=7E53583A0AB707DE486DADA0983DAEEA&appVersion=1.1.7&action=301&cmdName=app_zz&uuid=000000000000000&params={\"LottID\":97}";
        // System.out.println("url is " + url);
        result = url;
        return result;
    }

    /**
     * 执行投注接口
     *
     * @param playTypeId
     * @param number
     * @param money
     * @param lotID
     * @param issueID
     * @param issueName
     * @param userID
     * @param action
     * @return
     */
    public String getResponseOfBet(String playTypeId, String number, String money,
                                   int lotID, int issueID, String issueName,
                                   String userID, String action) {
        String result = "";
        JSONArray numberVal = this.getBetParamsString(playTypeId, number, money);
        String completeBetParams = this.getCompleteBetParams(lotID, issueID, issueName, numberVal, Double.valueOf(money));
        String paramsUrl = this.getPrefixBetParams(completeBetParams, userID, action);
        String url = baseService.prefixUrl + paramsUrl;

        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(url)
                .build();

        try {
            Response response = client.newCall(request).execute();
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取投注的URL
     *
     * @param playTypeId
     * @param number
     * @param money
     * @param lotID
     * @param issueID
     * @param issueName
     * @param userID
     * @param action
     * @return
     */
    public String getURLOfBet(String playTypeId, String number, String money,
                              int lotID, int issueID, String issueName,
                              String userID, String action) {
        String result;
        JSONArray numberVal = this.getBetParamsString(playTypeId, number, money);
        String completeBetParams = this.getCompleteBetParams(lotID, issueID, issueName, numberVal, Double.valueOf(money));
        String paramsUrl = this.getPrefixBetParams(completeBetParams, userID, action);
        String url = baseService.prefixUrl + paramsUrl;
        result = url;
        return result;
    }

    /**
     * 获取投注的URL
     *
     * @param numArr
     * @param totalMoney
     * @param lotID
     * @param issueID
     * @param issueName
     * @param userID
     * @param action
     * @return
     */
    public String getURLOfBet(List<Map<String, Object>> numArr, String totalMoney,
                              int lotID, int issueID, String issueName,
                              String userID, String action) {
        String result;
        JSONArray numberVal = this.getBetParamsStringList(numArr);
        String completeBetParams = this.getCompleteBetParams(lotID, issueID, issueName, numberVal, Double.valueOf(totalMoney));
        String paramsUrl = this.getPrefixBetParams(completeBetParams, userID, action);
        String url = baseService.prefixUrl + paramsUrl;
        result = url;
        return result;
    }

    /**
     * 获取冷热分析结果的URL
     *
     * @return
     */
    public String getURLOfColdAndHot() {
        String result;

        // System.out.println(GetJsonData.urlPath);
        String url = baseService.prefixUrl + "UserID=NzU2&platformVersion=6.0&platformCode=Android&sign=7E53583A0AB707DE486DADA0983DAEEA&appVersion=1.1.7&action=10316&cmdName=app_zz&uuid=000000000000000&params={}";
        result = url;
        return result;
    }

    /**
     * 获取某一彩种的提前封盘时间的URL
     *
     * @return
     */
    public String getURLOfAdjournment() {
        String result;

        // System.out.println(GetJsonData.urlPath);
        String url = baseService.prefixUrl + "UserID=NzU2&platformVersion=6.0&platformCode=Android&sign=7E53583A0AB707DE486DADA0983DAEEA&appVersion=1.1.7&action=10317&cmdName=app_zz&uuid=000000000000000&params={\"LottID\":97}";
        result = url;
        return result;
    }

    /**
     * 获取截止时间的URL
     * @return
     */
    public String getURLOfEndTime() {
        String result;

        // System.out.println(GetJsonData.urlPath);
        String url = baseService.prefixUrl + "UserID=NzU2&platformVersion=6.0&platformCode=Android&sign=7E53583A0AB707DE486DADA0983DAEEA&appVersion=1.1.7&action=200&cmdName=app_zz&uuid=000000000000000&params={\"LottID\":97}";
        result = url;
        return result;
    }

    /**
     * 获取服务器系统的时间的URL
     *
     * @return
     */
    public String getURLOfSystemTime() {
        String result;

        // System.out.println(GetJsonData.urlPath);
        String url = baseService.prefixUrl + "UserID=NzU2&platformVersion=6.0&platformCode=Android&sign=7E53583A0AB707DE486DADA0983DAEEA&appVersion=1.1.7&action=10318&cmdName=app_zz&uuid=000000000000000&params={}";
        result = url;
        return result;
    }

    /**
     * 获取投注记录的URL
     *
     * @param userId
     * @return
     */
    public String getURLOfBetRecord(String userId) {
        String result;
        String url = baseService.prefixUrl + "UserID=" + userId + "&platformVersion=6.0&platformCode=Android&sign=7E53583A0AB707DE486DADA0983DAEEA&appVersion=1.1.7&action=111&cmdName=app_zz&uuid=000000000000000&params={\"LotID\":97,\"PageNo\":1,\"PageSize\":10}";
        result = url;
        return result;
    }

}
